"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var rxjs_1 = require("rxjs");
var operators_1 = require("rxjs/operators");
var SocketRoom = /** @class */ (function () {
    function SocketRoom(id, msgType) {
        if (msgType === void 0) { msgType = ['joinRoom', 'sendToRoom', 'leaveRoom']; }
        this.msgType = [];
        this.roomId = id;
        this.roomName = "room" + this.roomId;
        this.msgType = msgType;
    }
    SocketRoom.prototype.join = function (socket, data) {
        var _this = this;
        socket.join(this.roomName);
        this.send(socket, { type: 'joinRoom', data: data });
        var msgs = this.msgType.map(function (res) {
            return rxjs_1.fromEvent(socket, res).pipe(operators_1.map(function (data) { return ({
                type: res,
                data: data
            }); }));
        });
        // 监听消息
        rxjs_1.merge.apply(void 0, msgs).pipe(
        // 处理消息 然后广播
        operators_1.tap(function (res) {
            _this.send(socket, res);
        }))
            .subscribe();
    };
    SocketRoom.prototype.send = function (socket, msg) {
        socket.to(this.roomName).emit(msg.type, msg.data);
        socket.emit(msg.type, msg.data);
    };
    return SocketRoom;
}());
exports.SocketRoom = SocketRoom;
